﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Qs.App.Base;
using Qs.App.Interface;
using Qs.App.SmsManager.Model;
using Qs.Comm;
using Qs.Comm.Cache;
using Qs.Repository;
using Qs.Repository.Domain;
using Qs.Repository.Interface;
using Qs.Repository.Vm;

namespace Qs.App
{
    /// <summary>
    /// 手机短信
    /// </summary>
    public class AppSendSms : AppBaseString<ModelUser, QsDBContext>
    {
        private AppStoreSetting _appSetting;
        private VmSettingSms smsSetting;
        private ISmsHelper sms;
        private ICacheContext _cacheContext;

        public AppSendSms(IUnitWork<QsDBContext> unitWork, IRepository<ModelUser, QsDBContext> repository,
            AppStoreSetting appSetting, ICacheContext cacheContext, DbExtension dbExtension, IAuth auth) : base(unitWork, repository, dbExtension, auth)
        {
            _appSetting = appSetting;
            _cacheContext = cacheContext;
            smsSetting = _appSetting.GetDetail("sms")??new VmSettingSms();
            switch (smsSetting.Default)
            {                 
                case "qcloud"://腾讯云
                    sms = new SmsTx(smsSetting);
                    break;
                case "aliyun"://阿里云短信
                  sms = new SmAli(smsSetting);
                    break;
                case "qiniu":  //七牛云
                    break;
                default:
                   
                    break;
            }
        }

        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="phone"></param>
        public ResPhoneCode SendCode(string phone)
        {
            
            if (sms == null)
            {
                throw new Exception("请先配置短信设置,店铺设置=>短信通知)");
            }

            string code = xConv.GenerateRandomCode(4);
            ResApiSms res = sms.SendPhoneCode(phone, code);
            string verifyCodeId = xConv.NewGuid();
            _cacheContext.Set(verifyCodeId, code, DateTime.Now.AddMinutes(5));
            ResPhoneCode resResult=new ResPhoneCode()
            {
                VerifyCodeId = verifyCodeId,
                // code = code,
            };
            return resResult; 
        }

      /// <summary>
      /// 送新订单(到商户)
      /// </summary>
      /// <param name="phone"></param>
      /// <param name="orderNo"></param>
        public void SendNewOrderToStore(string phone,string orderNo)
        {
             sms.SendNewOrderToStore(phone,orderNo);
        }
      
        

    }
}
